package top.cdmrx.demo.edu.controller;


import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.cdmrx.common.R;
import top.cdmrx.demo.edu.entity.Chapter;
import top.cdmrx.demo.edu.entity.chapter.ChapterVo;
import top.cdmrx.demo.edu.service.ChapterService;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-09-18
 */
@RestController
@RequestMapping("/edu/chapter")
@CrossOrigin
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    @PostMapping(value = "getChapterVideo/{courseId}")
    public R getChapterVideo(@PathVariable String courseId){

        List<ChapterVo> listChapter=chapterService.getChapterVideo(courseId);

        return R.success().data("chapterList",listChapter);
    }


    @DeleteMapping(value = "deleteChapter")
    public R deleteChapter(String chapterId){

        boolean b = chapterService.deleteChapter(chapterId);

        if (b){
            return R.success();
        }else {
            return R.fail();
        }
    }

    //添加章节
    @ApiOperation(value = "新增章节")
    @PostMapping
    public R save(
            @ApiParam(name = "chapter", value = "章节对象", required = true)
            @RequestBody Chapter chapter){
        chapterService.save(chapter);
        return R.success();
    }

    //根据章节id查询
    @ApiOperation(value = "根据ID查询章节")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id", value = "章节ID", required = true)
            @PathVariable String id){
        Chapter chapter = chapterService.getById(id);
        return R.success().data("chapter", chapter);
    }

    //修改章节
    @ApiOperation(value = "根据ID修改章节")
    @PutMapping("{id}")
    public R updateById(
            @ApiParam(name = "chapter", value = "章节对象", required = true)
            @RequestBody Chapter chapter){
        chapterService.updateById(chapter);
        return R.success();
    }

    //根据ID删除章节
    @ApiOperation(value = "根据ID删除章节")
    @DeleteMapping("{id}")
    public R removeById(
            @ApiParam(name = "id",value = "章节ID",required = true)
            @PathVariable String id
    ){

        boolean remove = chapterService.deleteChapter(id);
        if (remove){
            return R.success();
        }else {

            return R.fail();
        }
    }
}

